Free cookie consent management tool by TermsFeed Sending a HTTP HEAD using requests | Pythontic.com

Sending a HTTP HEAD using requests

Overview:

  • The head() method sends a http HEAD request to a resolved web server of a given domain name.
  • The head() method returns a response object containing the reply for the HEAD request.
  • If the response object is printed directly, the string representation of the object is printed in the format <Response [Status code]> e.g., <Response [200]>.
  • If a specific field from the response to the HEAD request is required, it can be obtained through the headers attribute of the request object.
  • Alternatively, the method request() can be invoked with a request type string as “HEAD”.

Example:

# Example Python program that sends a HTTP HEAD request
# to a host using the requests library
import requests

# Send a request to a HTTP port
response = requests.head("http://www.example.com")
print(response)

# Send a request to a HTTPS port
response = requests.head("https://example.com")
print(response)
print(type(response))
print(response.headers)
print(type(response.headers))

# The dictionary is a CaseInsensitiveDict
print(response.headers["Content-Type"])
print(response.headers["content-type"])

Output:

<Response [200]>
<Response [200]>
<class 'requests.models.Response'>
{'Accept-Ranges': 'bytes', 'Content-Type': 'text/html', 'ETag': '"bc2473a18e003bdb249eba5ce893033f:1760028122.592274"', 'Last-Modified': 'Thu, 09 Oct 2025 16:42:02 GMT', 'Vary': 'Accept-Encoding', 'Content-Encoding': 'gzip', 'Cache-Control': 'max-age=86000', 'Date': 'Sat, 18 Oct 2025 17:33:42 GMT', 'Content-Length': '20', 'Connection': 'keep-alive', 'Alt-Svc': 'h3=":443"; ma=93600'}
<class 'requests.structures.CaseInsensitiveDict'>
text/html
text/html

 


Copyright 2025 © pythontic.com